/*
 * Project: jcerticles
 * Copyright(c) 2011 jcerticles
 * All rights reserved.
 *
 * $Revision $, last modified $Date $ by $Author $
 */
package com.quintilis31.home.web.content.action.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;

import com.quintilis31.home.common.repository.content.Answer;
import com.quintilis31.home.common.repository.content.Question;
import com.quintilis31.home.core.content.facade.IContentFacade;
import com.quintilis31.home.core.content.util.ExamObject;
import com.quintilis31.home.web.content.action.IExamContentAction;
import com.quintilis31.home.web.content.model.ExamContentModel;

/**
 * TODO... Write javadoc...
 * @author jothi
 * @since 1.6
 */
public class ExamContentAction extends ContentAction implements IExamContentAction, SessionAware {
    
	/* The unique serial version identifier */
	private static final long serialVersionUID = 1L;
	
	/* The session object */
	private Map < String, Object > sessionMap;
	
	/* The model to be instantiated for all ExamContent related actions */
	private ExamContentModel examContentModel;
	
    /* The UserFacade reference */
    @Resource(name = "contentFacade")
    private IContentFacade contentFacade;	

	@Override
	public Object getModel() {
		return examContentModel;
	}

	@Override
	public void prepare() throws Exception {
        if (examContentModel == null) {
        	examContentModel = new ExamContentModel();
        }		
	}
	
	@Override
	public String execute() {
		return SUCCESS;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public String getExamQuestion() {
		return SUCCESS;		
	}
	
	/**
	 * {@inheritDoc}
	 */
	public String getAllQuestionsForExam() {
		System.out.println("The technologyId " + examContentModel.getTechnologyId());
		System.out.println("The sub technologyId " + examContentModel.getSubTechnologyId());
		/* Get the values for the technologyId, sub-technologyId, */
		long technologyId = examContentModel.getTechnologyId();
		long subTechnologyId = examContentModel.getSubTechnologyId();
		
		//List < Question > questionsList = contentFacade.getAllQuestionsForExam(technologyId, subTechnologyId);
		//System.out.println("The questions list is " + questionsList);
		/* Set the obtained questionList in session */
		// TODO... Prepare the ExamObject and stuff it in the session
		this.getSession().put("examObject", getExamObject());
		System.out.println("In getAllQuestionsForExam......Printing the session that was just created...");
		ExamObject exam = (ExamObject) this.getSession().get("examObject");
		System.out.println("Printing the question text..." + exam.getCurrentQuestion().getQuestionText());
		return SUCCESS;		
	}
	
	/**
	 * {@inheritDoc}
	 */
	public String startExam() {
		System.out.println("The technologyId " + examContentModel.getTechnologyId());
		System.out.println("The sub technologyId " + examContentModel.getSubTechnologyId());

		long technologyId = examContentModel.getTechnologyId();
		long subTechnologyId = examContentModel.getSubTechnologyId();
		
		/* Get an ExamObject */
		ExamObject examObj = contentFacade.createExamObject(technologyId, subTechnologyId);
		System.out.println("The ExamObject is " + examObj);
		
		/* Set the obtained examObject in session */
		// TODO... How to check if this is a new session. Here we always need a new session object.
		this.getSession().put("examObject", examObj);

		return SUCCESS;		
	}
	
	private ExamObject getExamObject() {

		ExamObject obj = new ExamObject();
		Question ques1 = new Question();
		ques1.setQuestionText("Which are allowed java variables...");
		ques1.setQuestionId(1L);
		ques1.setIsAnswered(true);
		ques1.setQuestionType('0');
		Question ques2 = new Question();
		ques2.setQuestionText("When to display this...");
		ques2.setQuestionId(2L);
		ques2.setIsAnswered(false);
		Question ques3 = new Question();
		ques3.setQuestionText("What to do after this...");
		ques3.setQuestionId(3L);
		ques3.setIsAnswered(false);
		obj.setCurrentQuestion(ques1);
		//obj.setCurrentQuestion(ques2);
		//obj.setCurrentQuestion(ques3);
		SortedMap  < Long, Long > map = new TreeMap < Long, Long > ();
		for (long i=0; i<10; i++) {
			map.put(10L+i, i);			
		}
		SortedMap < Question, List<Answer>> qBank = new TreeMap <Question, List<Answer>>();
		qBank.put(ques1, null);
		qBank.put(ques2, null);
		qBank.put(ques3, null);
		ques1.setIsAnswered(true);
		obj.setAllQuestionIds(map);
		obj.setQuestionBank(qBank);
		
		List<Answer> currentAnswerList = new ArrayList<Answer>();
		Answer ans1 = new Answer();
		ans1.setAnswerText("Public");
		ans1.setAnswerId(1L);
		Answer ans2 = new Answer();
		ans2.setAnswerText("Private");
		ans2.setAnswerId(2L);
		ans2.setIsAnswerSelectedByUser(true);
		Answer ans3 = new Answer();
		ans3.setAnswerText("Protected");
		ans3.setAnswerId(3L);
		Answer ans4 = new Answer();		
		ans4.setAnswerText("Default");
		ans4.setAnswerId(4L);
		currentAnswerList.add(ans1);
		currentAnswerList.add(ans2);
		currentAnswerList.add(ans3);
		currentAnswerList.add(ans4);
		
		obj.setCurrentAnswers(currentAnswerList);
		
		return obj;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public String getNextQuestion() {
		System.out.println("The technologyId " + examContentModel.getTechnologyId());
		System.out.println("The sub technologyId " + examContentModel.getSubTechnologyId());
		/* Get the values for the technologyId, sub-technologyId, */
		long technologyId = examContentModel.getTechnologyId();
		long subTechnologyId = examContentModel.getSubTechnologyId();
		
		//List < Question > questionsList = contentFacade.getAllQuestionsForExam(technologyId, subTechnologyId);
		//System.out.println("The questions list is " + questionsList);
		/* Set the obtained questionList in session */
		// TODO... Prepare the ExamObject and stuff it in the session
		// Check the session if there is a examObject in session scope...
		this.getSession().remove("examObject");
		if (this.getSession().get("examObject") != null) {
			System.out.println("In the if block ******************** Session exists...");
			ExamObject exam = (ExamObject) this.getSession().get("examObject");
			Question ques = exam.getCurrentQuestion();
			ques.setQuestionText("This is the second question... wow it worked...");
			exam.setCurrentQuestion(ques);
			this.getSession().put("examObject", exam);
		}
		else {
			System.out.println("In the else block ******************** Starting a new session...");			
			this.getSession().put("examObject", getExamObject());
		}
		
		return SUCCESS;
	}
	
	/**
	 * Called when the user finishes the exam
	 * @return
	 */
	public String finishAndEvaluateExam() {
		
		// TODO... Destroy the session object, and of course the examObject attribute in session
		ExamObject examObject = (ExamObject) this.getSession().get("examObject");
		/* Call the service to evaluate the exam */
		
		return SUCCESS;		
	}

	@Override
	public void setSession(Map < String, Object > sessionMap) {
		this.sessionMap = sessionMap;		
	}
	
	/**
	 * Get the current session
	 * @return
	 */
	private Map < String, Object > getSession() {
		return this.sessionMap;
	}
}